import { MODEL_NAME } from '../../constants/model-names';
import { EVENTS, type IEventBus } from '../../events';
import type { UserInfoModel } from '../../model';
import { BaseSystem } from '../base';
import type { PlayerStateSystem } from '../player-state';

export class UserInfoSystem extends BaseSystem {
  private bus: IEventBus;
  private playerStateSystem: PlayerStateSystem;

  constructor(bus: IEventBus, playerStateSystem: PlayerStateSystem) {
    super();
    this.bus = bus;
    this.playerStateSystem = playerStateSystem;

    this.bus.on(EVENTS.USER_INFO_CHANGE_USERNAME, (name) => {
      const userInfoModel = this.playerStateSystem.getModel<UserInfoModel>(
        MODEL_NAME.USER_INFO
      );
      userInfoModel.setUsername(name);
    });
  }
}
